package dao;

import utils.ConnectionManager;
import utils.Login;
import utils.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class LoginDao implements ILogin {
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rst;
    private ConnectionManager connectionManager = new ConnectionManager();

    public boolean insert(User user) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("INSERT INTO t_login(name,pass) VALUES (?,?)");
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    //根据user表的id删除login表对应的记录
    public boolean delete(int userid) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("DELETE t_login.* FROM t_login,t_user where t_user.name=t_login.name and t_user.userid=?");
            ps.setInt(1,userid);
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public boolean update(User user) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("UPDATE t_login,t_user set t_login.name=?,t_login.pass=? where t_user.name=t_login.name and t_user.userid=?");
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getUser_id());
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public Login findbyname(String name) {
        Login login=null;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_login where name=?");
            ps.setString(1,name);
            rst = ps.executeQuery();
            if(rst.next()){
                login = setLogin(rst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return login;
    }

    public ArrayList<Login> findAll() {
        ArrayList<Login> logins = new ArrayList<Login>();
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_login");
            rst = ps.executeQuery();
            while (rst.next()){
                Login login = setLogin(rst);
                logins.add(login);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return logins;
    }
    public Login setLogin(ResultSet rst) throws Exception {
        Login login = new Login();
        login.setUsername(rst.getString("name"));
        login.setPassword(rst.getString("pass"));
        login.setType(rst.getInt("type"));
        return login;
    }

    public int check(String username,String password){
        int type=-1;
        try{
            con=connectionManager.getCon();
            ps = con.prepareStatement("select type from t_login where name=? and pass=?");
            ps.setString(1,username);
            ps.setString(2,password);
            rst = ps.executeQuery();
            if (rst.next()){
                type=rst.getInt("type");
                System.out.println("type:"+type);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            connectionManager.closeAll(con,ps,rst);
        }
        return type;
    }
}
